function Hi5Player(config){
    this.tracks=[];
    this.currentTrack=-1;
    this.playButton='';
    this.timerDiv='';
    this.title='Hi5Player';
    this.titleDiv='';
    this.progressBar='';
    this.progress='';
    
    this.buildPlayer(config.container);
}
Hi5Player.prototype.addTrack = function(config) {
    track = document.createElement('audio');
    track.id = 'Hi5Track_'+config.name;
    track.src=config.name+"."+config.type;
    track.title=config.title;
    this.tracks.push(track);
    var player = this;
    track.addEventListener('timeupdate',function(){
        player.updateTimer(this);
        player.updateProgressBar(this);
    });
    return true;
}
Hi5Player.prototype.updateTitle = function(title){
    this.title=title;
    this.titleDiv.innerHTML=this.title;
}
Hi5Player.prototype.updateTimer = function(audio){
    currMin = Math.floor(audio.currentTime/60);
    currSec = Math.floor(audio.currentTime%60);
    if(currSec<10)currSec="0"+currSec.toString();
    
    totalMin = Math.floor(audio.duration/60);
    totalSec = Math.floor(audio.duration%60);
    if(totalSec<10)totalSec="0"+totalSec.toString();
    
    this.timerDiv.innerHTML=currMin+":"+currSec+" / "+totalMin+":"+totalSec;
}
Hi5Player.prototype.updateProgressBar = function(audio){
    var percent = ((audio.currentTime/audio.duration)*100);
    var width = ((this.progressBar.clientWidth*percent)/100);
    this.progress.style.width=width+"px";
}
Hi5Player.prototype.playTrack = function(n){
    if(n==this.currentTrack && this.currentTrack>-1){
        if(!(this.tracks[n].paused)){
            this.tracks[n].pause();
            this.playButton.id='Hi5Play';
            this.playButton.title='Play';
            this.updateTitle('');
            return true;
        }else{
            this.tracks[n].play();
            this.playButton.id='Hi5Pause';
            this.playButton.title='Pause';
            this.updateTitle(this.tracks[n].title);
            return true;
        }
    }
    if(n<this.tracks.length && n>-1){
        $(this.tracks).each(function(){
            this.pause();
            this.currentTime=0;
        });
        this.tracks[n].play();
            this.playButton.id='Hi5Pause';
        this.playButton.title='Pause';
        this.updateTitle(this.tracks[n].title);
        this.currentTrack=n;
        return true;
    }else{
        this.tracks[0].play();
            this.playButton.id='Hi5Pause';
        this.playButton.title='Pause';
        this.updateTitle(this.tracks[0].title);
        this.currentTrack=0;
    }
    return false;
}
Hi5Player.prototype.stopTrack = function(){
    this.tracks[this.currentTrack].pause();
    this.tracks[this.currentTrack].currentTime=0;
    this.playButton.id='Hi5Play';
    this.playButton.title='Play';
    this.updateTitle('');
    return true;
}
Hi5Player.prototype.playNext = function(){
    if(!((this.currentTrack+1)>=this.tracks.length))
    this.playTrack(parseInt(this.currentTrack)+1);
}
Hi5Player.prototype.playPrev = function(){
    if(this.currentTrack!=-1){
        if(this.tracks[this.currentTrack].currentTime<2){
            this.playTrack(parseInt(this.currentTrack)-1);
        }else{
            this.tracks[this.currentTrack].currentTime=0;
            return false;
        }
        return true;
    }
    return false;
}
Hi5Player.prototype.seekForward = function(n){
    n = n || 5;
    if((this.tracks[this.currentTrack].currentTime+n)>this.tracks[this.currentTrack].duration){
        this.tracks[this.currentTrack].currentTime=this.tracks[this.currentTrack].duration;
    }else{
        this.tracks[this.currentTrack].currentTime=this.tracks[this.currentTrack].currentTime+n;
    }
}
Hi5Player.prototype.seekReverse = function(n){
    n = n || 5;
    if((this.tracks[this.currentTrack].currentTime-n)<0){
        this.tracks[this.currentTrack].currentTime=0;
    }else{
        this.tracks[this.currentTrack].currentTime=this.tracks[this.currentTrack].currentTime-n;
    }
    console.log(this.tracks[this.currentTrack].currentTime);
}
Hi5Player.prototype.buildPlayer = function(div){
    var player = this;
    var b;//button

    //prev
    b = document.createElement('div');
    b.className = 'Hi5Button';
    b.id = 'Hi5Prev';
    b.title = 'Previous';
    b.onclick=function(){player.playPrev();}
    document.getElementById(div).appendChild(b);
    
    //seekReverse
    b = document.createElement('div');
    b.className = 'Hi5Button';
    b.id = 'Hi5SeekReverse';
    b.title = 'Seek Backwards';
    b.onclick=function(){player.seekReverse();}
    document.getElementById(div).appendChild(b);
    
    //play
    b = document.createElement('div');
    b.className = 'Hi5Button';
    b.id = 'Hi5Play';
    b.title = 'Play';
    b.onclick=function(){player.playTrack(player.currentTrack);}
    document.getElementById(div).appendChild(b);
    this.playButton = b;
    
    //stop
    b = document.createElement('div');
    b.className = 'Hi5Button';
    b.id = 'Hi5Stop';
    b.title = 'Stop';
    b.onclick=function(){player.stopTrack();}
    document.getElementById(div).appendChild(b);
    
    //seekforward
    b = document.createElement('div');
    b.className = 'Hi5Button';
    b.id = 'Hi5SeekForward';
    b.title = 'Seek Forward';
    b.onclick=function(){player.seekForward();}
    document.getElementById(div).appendChild(b);
    
    //next
    b = document.createElement('div');
    b.className = 'Hi5Button';
    b.id = 'Hi5Next';
    b.title = 'Next';
    b.onclick=function(){player.playNext();}
    document.getElementById(div).appendChild(b);
    
    var t;//timer
    t = document.createElement('div');
    t.id = 'Hi5Timer';
    document.getElementById(div).appendChild(t);
    this.timerDiv=t;
    
    t = document.createElement('div');
    t.id = 'Hi5Title';
    t.innerHTML=this.title;
    document.getElementById(div).appendChild(t);
    this.titleDiv=t;
    
    var pb;//progess bar
    pb=document.createElement('div');
    pb.id='Hi5ProgressBar';
    document.getElementById(div).appendChild(pb);
    this.progressBar=pb;
    
    var p;//progess
    p=document.createElement('div');
    p.id='Hi5Progress';
    pb.appendChild(p);
    this.progress=p;
}